package com.pickup.netty.server.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;


public class BinaryHandler extends SimpleChannelInboundHandler<BinaryWebSocketFrame> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, BinaryWebSocketFrame msg) throws Exception {
        System.out.println("字节处理");

        ByteBuf content = msg.content();

        System.out.println("服务器接收到二进制消息.:  "+content);

        content.markReaderIndex();

        int flag = content.readInt();

        System.out.println("标志位:[{}]"+ flag);

        content.resetReaderIndex();

        ByteBuf byteBuf = Unpooled.directBuffer(msg.content().capacity());

        byteBuf.writeBytes(msg.content());

        ctx.writeAndFlush(new BinaryWebSocketFrame(byteBuf));
    }
}
